<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Admin\Controller\FileController;
use User\Api\UserApi;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class SupplierController extends HomeController
{

    /**
     * 检测是否登录
     * */
    public function _initialize()
    {
        
        $action_name=ACTION_NAME;
        if($action_name == Login || $action_name == login || $action_name == confirm_login || $action_name == Confirm_login || $action_name == Index || $action_name == index){
            //登录 跟首页接口的时候不需要判断是否登录
        }elseif(empty(session('user_idd'))){
            // header('Location: ' . U('/Home/Supplier/Login'));exit;
            $this->error('请登录',U('Supplier/Login'));
        }else{
            //再来判断权限
            if(session('Supertube')==1){
                //超管 拥有所有权限

                //看商户有没有认证 没有认证就跳转去认证页面
                $action_name=ACTION_NAME;
                if($action_name == Book_index || $action_name == book_index || $action_name == Book_add || $action_name == book_add){
                    $supplier_id=session('supplier_id');
                    $api_status=M('supplier')->where("id=$supplier_id")->getField('api_status');

                    if($api_status !=1){
                        return $this->error('未认证，跳转到认证页面',U('Supplier/account_setup'));
                    }
                }

            }else{
                $id=session('user_idd');//不是超管的时候 user_id是account_number的主键ID
                $jurisdiction=M('supplier_account')->where("id=$id")->getField('jurisdiction');
                $action_name=ACTION_NAME;

                if($action_name == Supplier_index || $action_name == supplier_index){
                    //访问数据统计
                    $domain = strstr($jurisdiction, '1');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == Book_index || $action_name == book_index || $action_name == Book_add || $action_name == book_add){
                    //排行榜
                    $domain = strstr($jurisdiction, '2');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }else{
                        $supplier_id=session('supplier_id');
                        $api_status=M('supplier')->where("id=$supplier_id")->getField('api_status');

                        if($api_status !=1){
                            return $this->error('未认证，跳转到认证页面',U('Supplier/account_setup'));
                        }
                    }
                }elseif($action_name == Order_index || $action_name == order_index){
                    //线索列表
                    $domain = strstr($jurisdiction, '3');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == Transaction_index || $action_name == transaction_index){
                    //客户列表
                    $domain = strstr($jurisdiction, '4');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }
            }
        }

    }

    public function Login(){
        // exit('123');
        $this->display();
    }

    public function confirm_login()
    {
        if(IS_POST){

            $input = I('post.');

            $mobile = $input['mobile'];
            $password = $input['password'];

            if(empty($mobile)) api_return(2,'请输入账号');
            if(empty($password)) api_return(2,'请输入密码');

            $map['mobile'] = array('eq',$mobile);
            $map['user_type'] = 5;
            //此处iid代表供应商ID，不是机构ID
            $info = M('ucenter_member')->field('id,username,password,supplier_id')->where($map)->find();
            if($info){
                if(think_ucenter_md5($password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') == $info['password']){
                    $data['result'] = 1;
                    $data['msg'] = '登录成功';
                    session('user_idd', $info['id']);
                    session('name', $info['username']);
                    session('supplier_id', $info['supplier_id']);//供应商ID，后面到怎么添加供应商才确定
                    session('Supertube',1);//说明他是超管
                }else{
                    $data['result'] = 2;
                    $data['msg'] = '密码错误';
                }
            }else{
                //不是超管 我再判断是不是后台添加的管理员
                $condition['mobile']=$mobile;
                $res=M('supplier_account')->where($condition)->find();
                if($res){
                    if(think_ucenter_md5($password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') == $res['password']){
                        $data['result'] = 1;
                        $data['msg'] = '登录成功';
                        session('user_idd', $res['id']);//没有user_id 但是蒋磊做的时候每个页面都判断了user_id
                        session('supplier_id', $res['supplier_id']);
                        $name=$res['name'];
                        session('name',$name);
                        session('Supertube',2);//说明他不是超管
                    }else{
                        $data['result'] = 2;
                        $data['msg'] = '密码错误';
                    }
                }else{
                    $data['result'] = 2;
                    $data['msg'] = '账号不存在';                   
                }
            }
            echo json_encode($data);exit;
        }
    }
    //首页
    public function index(){
        $supplier_id=session('supplier_id');
        //待付款订单
        $unpaid_count=M('order')->where("supplier_id=$supplier_id && is_pay=0 && type=4")->count();
        //代发货
        $undelivered_count=M('order')->where("supplier_id=$supplier_id && status=1 && type=4")->count();
        //售后订单处理
        $sale_count=M('order')->where("supplier_id=$supplier_id && status=6 && type=4")->count();
        //已取消订单
        $con['supplier_id']=$supplier_id;
        $con['status']=array('between',"7,8");
        $con['type']=4;
        $cancelled_count=M('order')->where($con)->count();
        //已完成订单
        $completed_count=M('order')->where("supplier_id=$supplier_id && status=4 && type=4")->count();


        $this->assign('unpaid_count',$unpaid_count);
        $this->assign('undelivered_count',$undelivered_count);
        $this->assign('sale_count',$sale_count);
        $this->assign('cancelled_count',$cancelled_count);
        $this->assign('completed_count',$completed_count);
        $this->assign('cur','首页');
        $this->display();
    }
    public function index123(){
        $supplier_id=session('supplier_id');
    //     //待付款订单
        $unpaid_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.is_pay=0 && a.type=4")->select();
        $unpaid_count=count($unpaid_count);
        //代发货
        $undelivered_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.status=1 && a.type=4")->select();
        $undelivered_count=count($undelivered_count);
        //售后订单处理
        $sale_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.status=6 && a.type=4")->select();
        $sale_count=count($sale_count);
        //已取消订单
        $cancelled_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.status=8 && a.type=4")->select();
        $cancelled_count=count($cancelled_count);
        //已完成订单
        $completed_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.status=4 && a.type=4")->select();
        $completed_count=count($completed_count);

        $this->assign('unpaid_count',$unpaid_count);
        $this->assign('undelivered_count',$undelivered_count);
        $this->assign('sale_count',$sale_count);
        $this->assign('cancelled_count',$cancelled_count);
        $this->assign('completed_count',$completed_count);
        $this->assign('cur','首页');
        $this->display();
    }
    //供应商管理
    public function supplier_index(){
        $map['supplier_id']=session('supplier_id');
        $_REQUEST['mobile']?$map['mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $list=M('supplier_account')->where($map)->select();
        foreach($list as $k=>$v){
            $jurisdiction=explode(',',$v['jurisdiction']);
            foreach($jurisdiction as $kk=>$vv){
                if($vv==1){
                    $ju.=',供应商管理';
                }
                if($vv==2){
                    $ju.=',书籍管理';
                }
                if($vv==3){
                    $ju.=',交易管理';
                }
                if($vv==4){
                    $ju.=',收益管理';
                }
            }
            $list[$k]['ju']=ltrim($ju,',');
            unset($ju);
        }
        $this->assign('list',$list);
        $this->assign('mobile',$_REQUEST['mobile']);
        $this->assign('cur','供应商管理');
        $this->display();
    }
    //新增账号
    public function account_add(){
        $supplier_id=session('supplier_id');//供应商ID
        if($_POST){
            $data=$_POST;
            if(!$data['id']){
                $info['mobile']=$data['mobile'];
                if($data['password'] !==$data['confirm_password']){
                    return $this->error('密码跟确认密码不一致',U('account_add'));
                }
                $info['password']=think_ucenter_md5($data['password'], '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
                $info['name']=$data['name'];
                $jurisdiction=$data['jurisdiction'];
                foreach($jurisdiction as $k=>$v){
                    $info['jurisdiction'].=','.$v;
                }
                $info['supplier_id']=$supplier_id;
                $info['jurisdiction']=ltrim($info['jurisdiction'],',');
                $res=M('supplier_account')->add($info);
                if($res){
                    return $this->success('成功',U('supplier_index'));
                }else{
                    return $this->error('失败',U('supplier_index'));
                }
            }else{
                $info['mobile']=$data['mobile'];
                $info['name']=$data['name'];
                $jurisdiction=$data['jurisdiction'];
                foreach($jurisdiction as $k=>$v){
                    $info['jurisdiction'].=','.$v;
                }
                $info['jurisdiction']=ltrim($info['jurisdiction'],',');

                $id=$data['id'];

                $res=M('supplier_account')->where("id=$id")->save($info);
                if($res){
                    return $this->success('成功',U('supplier_index'));
                }else{
                    return $this->error('失败',U('supplier_index'));
                }
            }
        }
        $id=$_REQUEST['id'];//对象ID 有的话就是编辑
        if($id){
            $info=M('supplier_account')->where("id=$id")->find();
            $jurisdiction=explode(',',$info['jurisdiction']);
            foreach($jurisdiction as $k=>$v){
                if($v==1){
                    $a=1;
                }
                if($v==2){
                    $b=1;
                }
                if($v==3){
                    $c=1;
                }
                if($v==4){
                    $d=1;
                }
            }
            $this->assign('a',$a);
            $this->assign('b',$b);
            $this->assign('c',$c);
            $this->assign('d',$d);
            $this->assign('info',$info);
        }
        $this->assign('cur','供应商管理');
        $this->display();
    }
    public function account_del(){
        $id=$_REQUEST['id'];
        $res=M('supplier_account')->where("id=$id")->delete();
        if($res){
            $this->success('删除成功',U('Supplier/supplier_index'));
        }else{
            $this->error('删除失败',U('Supplier/supplier_index'));
        }
    }
    //账号设置
    public function account_setup(){
        $supplier_id=session('supplier_id');
        $uid = session("user_idd");//这里要注意一下
        if($_POST){

                $supplier_info = D('supplier')->where(array('uid' => $uid))->find();
                $data['name'] = $_REQUEST['name'];
                $data['mobile'] = $_REQUEST['mobile'];
                $data['IDcard'] = $_REQUEST['IDcard'];
                $data['card_front'] = $_REQUEST['card_front'];
                $data['card_behind'] = $_REQUEST['card_behind'];
                $data['hold_card'] = $_REQUEST['hold_card'];

                $data['bank_id'] = $_REQUEST['bank_id'];
                $data['bank_num'] = $_REQUEST['bank_num'];

                if ($data['name'] == '') {
                    $this->error("请输入负责人姓名");
                }

                if ($data['mobile'] == '') {
                    $this->error("请输入负责人联系方式");
                }
                if ($_REQUEST['code'] == '') $this->error("请输入验证码");

                if ($data['IDcard'] == '') {
                    $this->error("请输入负责人身份证号码");
                }
                if (empty($data['bank_id'])) $this->error('请选择开户行');
                if (empty($data['bank_num'])) $this->error('请填写银行卡号');

                if (empty($data['card_front'])) {
                    $this->error("请上传身份证正面");
                }

                if (empty($data['card_behind'])) {
                    $this->error("请上传身份证反面");
                }

                if (empty($data['hold_card'])) {
                    $this->error("请上传手持身份证");
                }


                $model = M();
                $model->startTrans();

                $time = time();
                $data['update_time'] = $time;
                if (empty($supplier_info)) {
                    $data['uid'] = $uid;
                    $data['create_time'] = $time;
                    $result = $model->table('teach_supplier')->add($data);
                } else {
                    $result = $model->table('teach_supplier')->where(array('uid' => $uid))->save($data);
                }

                Vendor('pay.pay');

                $user_info = array(
                    'user_name' => $data['name'],
                    'cert_id' => $data['IDcard'],
                    'user_mobile' => $data['mobile'],
                    'bank_id' => $data['bank_id'],
                    'card_no' => $data['bank_num'],
                    'vali_date' => '99991231'
                );

                if ($supplier_info['sub_mch_code']) {
                    $status_data = array(
                        'sub_mch_code' => $supplier_info['sub_mch_code']
                    );

                    $status_obj = new \pay($status_data);
                    $status_info = $status_obj->status();
                    // print_r($status_info);die;
                    if ($status_info['data']['status'] != 2) {
                        /***************上传身份证正反面，手持身份证******************/
                        //身份证正面图片
                        $card_front_path = M('picture')->where(array('id' => $data['card_front']))->getField('path');
                        $card_behind_path = M('picture')->where(array('id' => $data['card_behind']))->getField('path');
                        $card_info = array(
                            'identity_front' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_front_path)),
                            'identity_contrary' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_behind_path)),
                        );

                        //手持身份证图片
                        $hold_card_path = M('picture')->where(array('id' => $data['hold_card']))->getField('path');
                        $hold_card_info = array(
                            'person_with_id_photo' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $hold_card_path))
                        );
                        uploadCardInfo($supplier_info['sub_mch_code'], $user_info, $card_info, $hold_card_info);
                        /***************上传身份证正反面，手持身份证******************/
                    }
                } else {
                    // print_r($user_info);die;
                    if ($_REQUEST['sms_id'] && $_REQUEST['code']) {
                        $obj_data = array(
                            'user_info' => json_encode($user_info),
                            'merchant_type' => 2
                        );
                        $obj = new \pay($obj_data);
                        $merchant_info = $obj->merchant($_REQUEST['code'], $_REQUEST['sms_id']);
                        if ($merchant_info['result'] == 1) {
                            //这个地方merchant_code就是sub_mch_code
                            $sub_mch_code = $merchant_info['data']['merchant_code'];
                            $code_res = $model->table('teach_supplier')->where(array('id' => $supplier_info['id']))->setField('sub_mch_code', $sub_mch_code);

                            if ($code_res) {
                                /***************上传身份证正反面，手持身份证******************/
                                //身份证正面图片
                                $card_front_path = M('picture')->where(array('id' => $data['card_front']))->getField('path');
                                $card_behind_path = M('picture')->where(array('id' => $data['card_behind']))->getField('path');
                                $card_info = array(
                                    'identity_front' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_front_path)),
                                    'identity_contrary' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_behind_path)),
                                );

                                //手持身份证图片
                                $hold_card_path = getPicture($data['hold_card']);
                                $hold_card_info = array(
                                    'person_with_id_photo' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $hold_card_path))
                                );
                                uploadCardInfo($sub_mch_code, $user_info, $card_info, $hold_card_info);
                                /***************上传身份证正反面，手持身份证******************/
                            }
                        }else{
                            $this->error($merchant_info['message']);
                        }
                    }
                }

                if ($result) {
                    $model->commit();
                    $this->success("保存成功");
                } else {
                    $model->rollback();
                    $this->error("保存失败");
                }




                // $data=$_POST;
                // $data['update_time']=time();
                // $res=M('supplier')->where("id=$supplier_id")->save($data);

            if($res){
                return $this->success('成功',U('Supplier/account_setup'));
            }else{
                return $this->error('失败',U('Supplier/account_setup'));
            }
        }

        $info=M('supplier')->where("id=$supplier_id")->find();


        $info['card_front_path'] = D('picture')->where(array('id' => $info['card_front']))->getField('path');
        $info['card_behind_path'] = D('picture')->where(array('id' => $info['card_behind']))->getField('path');
        $info['hold_card_path'] = getPicture($info['hold_card']);


        Vendor('pay.pay');

        $obj_data = array(
            'sub_mch_code' => $info['sub_mch_code']
        );
        $obj = new \pay($obj_data);
        $status_info = $obj->status();
        $upload_status = $status_info['data']['status'];
        // print_r($upload_status);die;
        $this->assign('upload_status', $upload_status);



        $this->assign('info',$info);
        
        $bank_list = M('bank')->order('bank_id asc')->select();
        $this->assign('bank_list', $bank_list);
        
        $this->assign('cur','账户设置');
        $this->display();
    }


    public function sendSms()
    {
        $input = I('post.');

        $mobile = $input['mobile'];

        if (empty($mobile)) api_return(2, '请输入联系手机');

        Vendor('pay.pay');

        $data = array(
            'user_mobile' => $mobile,
        );
        $obj = new \pay($data);
        $sms_result = $obj->sms();
        if ($sms_result['result'] == 1) {
            api_return(1, '获取数据成功', $sms_result['data']['sms_id']);
        } else {
            api_return(2, '短信发送失败');
        }
    }

    //书籍管理
    public function book_index(){
        $map['is_del']=0;
        $map['supplier_id']=session('supplier_id');//先把供应商ID写死
        $_REQUEST['name']?$map['name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);

        $count = M('goods')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list = M('goods')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->order('id desc')->select();
        foreach($list as $k=>$v){
            $img=explode(',', $v['imgs']);
            $imgg=$img[0];
            if($imgg){
                $list[$k]['img']=M('picture')->where("id=$imgg")->getField('path');
            }else{
                $list[$k]['img']='';
            }
        }
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);
        $this->assign("list", $list);
        $this->assign('cur','书籍管理');
        $this->display();
    }
    //发布书籍
    public function book_add(){
        $supplier_id=session('supplier_id');
        $type_list=M('goods_type')->where('pid=0')->select();
        $id=M('goods')->order("id desc")->getField('id');
        $iid=$id+1;


                //父级分类
        $parent = D('goods_type')->where(array('pid' => 0))->order('id desc')->select();
        //子级分类
        $child = D('goods_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
        //课程
        $course = D('goods')->where(array('supplier_id' => $supplier_id, 'two_type_id' => $child[0]['id'], 'is_del' => 0))->order('id desc')->select();
        foreach ($course as $k => $v) {
            $img=explode(',', $v['imgs']);
            $imgg=$img[0];
            if($imgg){
                $course[$k]['img']=M('picture')->where("id=$imgg")->getField('path');
            }else{
                $course[$k]['img']='';
            }
        }
        $this->assign('parent', $parent);
        $this->assign('child', $child);
        $this->assign('course', $course);
        $this->assign('iid',$iid);
        $this->assign('type_list',$type_list);
        $this->assign('cur','书籍管理');
        $this->display();
    }
    public function book_edit(){
        $supplier_id=session('supplier_id');
        $ss=$_REQUEST['id'];
        $info=M('goods')->where("id=$ss")->find();
        $type_list=M('goods_type')->select();

        $info=M('goods')->where("id=$ss")->find();

        $attr_list123=M('item_attr_val')->where("item_id=$ss")->field('attr_key_id')->order('symbol asc')->select();

        foreach($attr_list123 as $k=>$v){
            if($k==0){
                $io.=$v['attr_key_id'];
            }else{
                $io.=','.$v['attr_key_id'];
            }
        }

        $io=explode(',', $io);
        $io=array_unique($io);
        $attr_list=array_merge($io);
 
        foreach($attr_list as $k=>$v){
            $attr_list1[$k]['attr_name']=M('item_attr_key')->where("attr_key_id=$v")->getField('attr_name');
        }

        $sku_list=M('goods_spec')->where("goods_id=$ss")->order('id asc')->select();
        foreach($sku_list as $k=>$v){
            $sku=json_decode($v['spec'],true);
            foreach($sku as $kk=>$vv){
                $sku[$kk]=$vv;
            }
            $sku_list[$k]['list']=$sku;
            if($v['img']){
                $sku_list[$k]['imgid']=$v['img'];
                $sku_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] . M('picture')->where("id=$v[img]")->getField('path');
            }else{
                $sku_list[$k]['img']='';
                $sku_list[$k]['imgid']=0;
            }
        }

        $item_id=$ss;
        $data=M('item_attr_key')->where(['item_id'=>$item_id])->order('attr_key_id asc')->select();
        $need=[];
        foreach($data as $k=>$v){
            $need[$k]=$v;
            $need[$k]['itemattrval'] =M('item_attr_val')->where("attr_key_id=$v[attr_key_id]")->order('symbol asc')->select();
        }
        $sku=M('goods_spec')->where(['goods_id'=>$item_id])->order('id asc')->select();
        $skus=[];
         foreach($sku as $k=>$v){
            $skus[$k] = $v;
        }
        //图片ID传过去，到时保存的时候又传过来，可能会用到
        $img_list=M('goods_spec')->where("goods_id=$ss")->field('img')->order('id asc')->select();
        foreach($img_list as $k=>$v){
            $imgg.=','.$v['img'];
        }
        $imgg=substr($imgg,1);
        //传规格表的ID过去，编辑保存的时候去比对，保存规格图片需要用到
        $symbol_list=M('item_attr_val')->where("item_id=$ss")->field('symbol')->order('symbol asc')->select();
        foreach($symbol_list as $k=>$v){
            $symboll.=','.$v['symbol'];
        }
        $symboll=substr($symboll,1);
        $this->assign('imgg',$imgg);
        $this->assign('symboll',$symboll);
        $this->assign('itemAttr',$need);
        $this->assign('itemSku',json_encode($skus,320));


        $imgss=explode(',', $info['imgs']);
        foreach($imgss as $k=>$v){
            $imgsss[$k]['path']=M('picture')->where("id=$v")->getField('path');
        }
        $this->assign('imgss', $imgsss);



                //父级分类
        $parent = D('goods_type')->where(array('pid' => 0))->order('id desc')->select();
        //子级分类
        $child = D('goods_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
        //课程
        $course = D('goods')->where(array('supplier_id' => $supplier_id, 'two_type_id' => $child[0]['id'], 'is_del' => 0))->order('id desc')->select();
        foreach ($course as $k => $v) {
            $img=explode(',', $v['imgs']);
            $imgg=$img[0];
            if($imgg){
                $course[$k]['img']=M('picture')->where("id=$imgg")->getField('path');
            }else{
                $course[$k]['img']='';
            }
        }
        $this->assign('parent', $parent);
        $this->assign('child', $child);
        $this->assign('course', $course);
        $types=M('goods')->where("id=$ss")->getField('type');
        if($types==2){
            $good_ids=M('goods')->where("id=$ss")->getField('good_ids');
            if($good_ids){
                $ids=explode(',', $good_ids);
                $good_list=array();
                foreach($ids as $k=>$v){
                    $imgk=M('goods')->where("id=$v")->getField('imgs');
                    $img=explode(',', $imgk);
                    $imgg=$img[0];
                    $good_list[$k]=M('goods')->where("id=$v")->find();
                    if($imgg){
                        $good_list[$k]['img']=M('picture')->where("id=$imgg")->getField('path');
                    }else{
                        $good_list[$k]['img']='';
                    }
                }
            }
            
        }

        $this->assign('good_list',$good_list);
        $this->assign('attr_list',$attr_list1);
        $this->assign('sku_list',$sku_list);
        $this->assign('iid',$ss);
        $this->assign('type_list',$type_list);
        $this->assign('cur','书籍管理');
        $this->assign('info',$info);
        $this->display();
    }
    public function book_del(){
        $id=$_REQUEST['id'];
        $res=M('goods')->where("id=$id")->save(['is_del'=>1]);
        if($res){
            $this->success('成功',U('book_index'));
        }else{
            $this->error('失败',U('book_index'));
        }
    }
    public function type_list(){
        $id=$_POST['type_id'];
        $info=M('goods_type')->where("pid=$id")->select();
        if($info){
            $value=['code'=>'200','info'=>$info,'msg'=>'二级分类'];
            exit(json_encode($value));   
        }else{
            $value=['code'=>'100','msg'=>'报错'];
            exit(json_encode($value));  
        }
    }
    //书籍规格
    public function book_spec(){
        $this->assign('cur','书籍规格');
        $this->display();
    }
        //交易管理
    public function order_index123(){
        $supplier_id=session('supplier_id');
        $map['a.supplier_id']=session('supplier_id');
        if($_REQUEST['name']){
            //当传了搜索值过来的时候，判断是什么类型
            if($_REQUEST['type']==1){
                //订单号
                $map['b.order_sn']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==2){
                //收货人姓名
                $map['b.get_username']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==3){
                //收货人手机号
                $map['b.get_mobile']=array('like',"%$_REQUEST[name]%");
            }
        }
        $_REQUEST['status']?$map['b.status']=$_REQUEST['status']:'';

        $count=M('order b')->join('teach_vice_order a on a.order_id=b.id')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $map['a.supplier_id']=$supplier_id;
        $list=M('order b')->join('teach_vice_order a on a.order_id=b.id')->group('b.id')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('b.id desc')->field('b.*')->select();
        // echo '<pre>';print_r($list);die;
        // $list=M('order')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            foreach($list[$k]['good_list'] as $kk=>$vv){
                $list[$k]['good_list'][$kk]['img']=M('picture')->where("id=$vv[img]")->getField('path');
            }
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
            if($v['status']==0){
                $list[$k]['status']='未付款';
            }elseif($v['status']==1){
                $list[$k]['status']='待发货';
            }elseif($v['status']==2){
                $list[$k]['status']='待收货';
            }elseif($v['status']==3){
                $list[$k]['status']='待评价';
            }elseif($v['status']==4){
                $list[$k]['status']='已完成';
            }elseif($v['status']==5){
                $list[$k]['status']='已过期';
            }elseif($v['status']==6){
                $list[$k]['status']='售后';
            }elseif($v['status']==7){
                $list[$k]['status']='订单取消退款中';
            }elseif($v['status']==8){
                $list[$k]['status']='已取消';
            }
        }

        $supplier_id=session('supplier_id');
        //订单数量
        $order_count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.type=4")->field('a.*')->select();
        $order_count=count($order_count);
        //售后订单
        $order_sale=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.type=4 && a.status=6")->field('a.*')->select();
        $order_sale=count($order_sale);
        //收款金额
        $order_list=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where("b.supplier_id=$supplier_id && a.type=4")->field('a.*')->select();
        $total_price=0;
        foreach($order_list as $k => $v){
            $total_price +=$v['pay_price'];
        }

        $this->assign('cur','交易管理');
        $this->assign('list',$list);
        $this->assign('order_count',$order_count);
        $this->assign('order_sale',$order_sale);
        $this->assign('total_price',$total_price);
        $this->assign('status',$_REQUEST['status']);
        $this->assign('type',$_REQUEST['type']);
        $this->assign('name',$_REQUEST['name']);
        $this->assign('_page',$show);
        $this->display();
    }

        //交易管理(弃用) 供应商ID他放在订单副表
    public function order_index(){
        $map['supplier_id']=session('supplier_id');
        $map['type']=4;//商城订单
        if($_REQUEST['name']){
            //当传了搜索值过来的时候，判断是什么类型
            if($_REQUEST['type']==1){
                //订单号
                $map['order_sn']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==2){
                //收货人姓名
                $map['get_username']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==3){
                //收货人手机号
                $map['get_mobile']=array('like',"%$_REQUEST[name]%");
            }
        }
        $_REQUEST['status']?$map['status']=$_REQUEST['status']:'';

        $count=M('order')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();


        $list=M('order')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            foreach($list[$k]['good_list'] as $kk=>$vv){
                $list[$k]['good_list'][$kk]['img']=M('picture')->where("id=$vv[img]")->getField('path');
            }
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
            if($v['status']==0){
                $list[$k]['status']='未付款';
            }elseif($v['status']==1){
                $list[$k]['status']='待发货';
            }elseif($v['status']==2){
                $list[$k]['status']='待收货';
            }elseif($v['status']==3){
                $list[$k]['status']='待评价';
            }elseif($v['status']==4){
                $status=M('after')->where("order_id=$v[id]")->order('id desc')->getField('status');
                if($status){
                    if($status==3){
                        $list[$k]['status']='售后失败';
                    }elseif($status==2){
                        $list[$k]['status']='售后成功';
                    }
                }else{
                    $list[$k]['status']='已完成';
                }
                
            }elseif($v['status']==5){
                $list[$k]['status']='已过期';
            }elseif($v['status']==6){
                $list[$k]['status']='售后';
            }elseif($v['status']==7){
                $list[$k]['status']='订单取消退款中';
            }elseif($v['status']==8){
                $list[$k]['status']='已取消';
            }
        }

        $supplier_id=session('supplier_id');
        //订单数量
        $order_count=M('order')->where("supplier_id=$supplier_id && type=4")->count();
        //售后订单
        $order_sale=M('order')->where("supplier_id=$supplier_id && type=4 && status=6")->count();
        //收款金额
        $con['a.supplier_id']=$supplier_id;
        $con['a.type']=4;
        $con['a.status']=array('between',"1,6");//大于0，小于7  售后也算，只要没同意

        $con['b.status']=array('neq',2);

        // $order_list=M('order')->where($con)->select();
        $order_list=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($con)->field('a.*')->select();
        $total_price=0;
        foreach($order_list as $k => $v){
            $total_price +=$v['pay_price'];
        }

        $this->assign('cur','交易管理');
        $this->assign('list',$list);
        $this->assign('order_count',$order_count);
        $this->assign('order_sale',$order_sale);
        $this->assign('total_price',$total_price);
        $this->assign('status',$_REQUEST['status']);
        $this->assign('type',$_REQUEST['type']);
        $this->assign('name',$_REQUEST['name']);
        $this->assign('_page',$show);
        $this->display();
    }




    //发货订单(弃用)
    public function order_delivery(){
        $map['supplier_id']=session('supplier_id');
        $map['type']=4;
        $_REQUEST['status']?$map['status']=$_REQUEST['status']:'';
        if(!$map['status']){
            $map['status']= array('between',"1,2");
        }
        $_REQUEST['name'] ? $map['order_sn'] = array('like', "%$_REQUEST[name]%") : '';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime = strtotime($_REQUEST['start_time']);
            $etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
            $map['create_time'] = array('between', "$btime,$etime");
        }
        // print_r($map);die;
        $count=M('order')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $list=M('order')->where($map)->limit($Page->firstRow.','.$Page->listRows)
        ->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            foreach($list[$k]['good_list'] as $kk=>$vv){
                $list[$k]['good_list'][$kk]['img']=M('picture')->where("id=$vv[img]")->getField('path');
            }
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);
        $this->assign('cur','发货订单');
        $this->display();
    }
    public function order_delivery123(){
        $map['b.supplier_id']=session('supplier_id');
        $map['a.type']=4;
        $_REQUEST['status']?$map['status']=$_REQUEST['status']:'';
        if(!$map['status']){
            $map['a.status']= array('between',"1,2");
        }
        $_REQUEST['name'] ? $map['a.order_sn'] = array('like', "%$_REQUEST[name]%") : '';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime = strtotime($_REQUEST['start_time']);
            $etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
            $map['a.create_time'] = array('between', "$btime,$etime");
        }
        $count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        // $list=M('order')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
         $list=M('order a')->join('teach_vice_order b on b.order_id=a.id')->group('a.id')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('a.id desc')->field('a.*')->select();
        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            foreach($list[$k]['good_list'] as $kk=>$vv){
                $list[$k]['good_list'][$kk]['img']=M('picture')->where("id=$vv[img]")->getField('path');
            }
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);
        $this->assign('cur','发货订单');
        $this->display();
    }
    //设置发货
    public function manage_deliver(){
        $id=$_REQUEST['id'];
        if($_POST){
            $data['shipper_id']=$_POST['shipper_id'];
            $data['shipper_code']=$_POST['shipper_code'];
            $data['id']=$_POST['id'];
            $data['status']=2;
            $res=M('order')->save($data);
            if($res){
                return $this->success('成功',U('order_delivery'));
            }else{
                return $this->error('失败',U('order_delivery'));
            }
        }
        $province=M('order')->where("id=$id")->getField('get_province');
        $city=M('order')->where("id=$id")->getField('get_city');
        $area=M('order')->where("id=$id")->getField('get_area');
        $address['address']=$province.$city.$area;
        $address['address_info']=M('order')->where("id=$id")->getField('get_address');
        $address['get_username']=M('order')->where("id=$id")->getField('get_username');
        $address['get_mobile']=M('order')->where("id=$id")->getField('get_mobile');
        $express_list=M('shipper')->select();
        $shipper_id=M('order')->where("id=$id")->getField('shipper_id');
        $shipper_code=M('order')->where("id=$id")->getField('shipper_code');
        $this->assign('express_list',$express_list);
        $this->assign('address',$address);
        $this->assign('shipper_id',$shipper_id);
        $this->assign('shipper_code',$shipper_code);
        $this->assign('id',$id);
        $this->assign('cur','发货订单');
        $this->display();
    }
        public function dc(){
        $data=I();

        $map['b.supplier_id']=session('supplier_id');
        $map['b.type']=4;
        if($data['start_time'] && $data['end_time']){
            $btime=strtotime($data['start_time']);
            $etime=strtotime($data['end_time']);
            $map['b.create_time'] = array('between',"$btime,$etime");            
        }
        $data['name']?$map['b.order_sn']=array('like',"%$data[name]%"):'';
        $data['status']?$map['b.status']=$data['status']:'';
        if(!$map['b.status']){
            $map['b.status']= array('between',"1,2");
        }

        $order_list=M('vice_order a')->join('teach_order b on a.order_id=b.id')->where($map)->select();
// print_r($order_list);die;
        foreach($order_list as $k=>$v){
            if($v['create_time']){
                $order_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            }else{
                $order_list[$k]['create_time']='';
            }
            if($v['status'] == 0){
                $order_list[$k]['statuss']='待付款';
            }else if($v['status'] == 1){
                $order_list[$k]['statuss']='待发货';
            }else if($v['status'] == 2){
                $order_list[$k]['statuss']='待收货';
            }else if($v['status'] == 3){
                $order_list[$k]['statuss']='待评价';
            }else if($v['status'] == 4){
                $order_list[$k]['statuss']='已完成';
            }else if($v['status'] == 5){
                $order_list[$k]['statuss']='已过期';
            }else if($v['status'] == 6){
                $order_list[$k]['statuss']='售后';
            }else if($v['status'] == 7){
                $order_list[$k]['statuss']='订单取消退款中';
            }else if($v['status'] == 8){
                $order_list[$k]['statuss']='已取消';
            }

            if($v['pay_type']==2){
                $order_list[$k]['pay_type']='支付宝';
            }else{
                $order_list[$k]['pay_type']='微信';
            }
            $order_list[$k]['nickname']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $order_list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
        }


        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">商品信息</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="100">订单编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">下单时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">下单人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">收货人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">实收款</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">支付方式</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单状态</td>';
        $strTable .= '</tr>';
        foreach($order_list as $k=>$val){
          $strTable .= '<tr>';
          $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['name'].'</td>';
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_sn'].' </td>';              
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].'</td>';  
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['nickname'].'<br>'.$val['mobile'].'</td>';
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['get_username'].'<br>'.$val['get_username'].'</td>';
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_price'].'</td>';
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_type'].'</td>';
          $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['statuss'].'</td>';
          $strTable .= '</tr>';
        }
        $strTable .='</table>';
        unset($order_list);
        $this->downloadExcel($strTable,'decoration');
        exit();
    }

    function downloadExcel($strTable,$filename){
        header("Content-type: application/vnd.ms-excel");
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
        header('Expires:0');
        header('Pragma:public');
        echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
    }
    //售后订单
    public function order_sale(){
        $map['b.supplier_id']=session('supplier_id');
        $map['b.type']=4;
        $map['b.status']=6;
        if($_REQUEST['name']){
            //当传了搜索值过来的时候，判断是什么类型
            if($_REQUEST['type']==1){
                //订单号
                $map['b.order_sn']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==2){
                //收货人姓名
                $map['b.get_username']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==3){
                //收货人手机号
                $map['b.get_mobile']=array('like',"%$_REQUEST[name]%");
            }
        }

        if($_REQUEST['status']){
            if($_REQUEST['status']==11){
                $map['a.status']=array('eq',0);
            }else{
                $map['a.status']=array('gt',0);
            }
        }
        $count=M('after a')->join('teach_order b on a.order_id=b.id')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $list=M('after a')->join('teach_order b on a.order_id=b.id')->limit($Page->firstRow.','.$Page->listRows)->field('a.*,b.id ids,b.order_sn,b.pay_price,b.get_mobile,b.get_username')->where($map)
        ->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[order_id]")->select();
            foreach($list[$k]['good_list'] as $kk=>$vv){
                $list[$k]['good_list'][$kk]['img']=M('picture')->where("id=$vv[img]")->getField('path');
            }
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            if($v['status']==0){
                $list[$k]['statuss']='未处理';
            }elseif($v['status']==1){
                $list[$k]['statuss']='售后中';
            }elseif($v['status']==2){
                $list[$k]['statuss']='售后成功';
            }elseif($v['status']==3){
                $list[$k]['statuss']='售后失败';
            }
        }
        // echo '<pre>';print_r($list);die;
        $this->assign('type',$_REQUEST['type']);
        $this->assign('name',$_REQUEST['name']);
        $this->assign('status',$_REQUEST['status']);
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);
        $this->assign('cur','售后订单');
        $this->display();
    }
    public function order_sale123(){
        $map['c.supplier_id']=session('supplier_id');
        $map['b.type']=4;
        $map['b.status']=6;
        if($_REQUEST['name']){
            //当传了搜索值过来的时候，判断是什么类型
            if($_REQUEST['type']==1){
                //订单号
                $map['b.order_sn']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==2){
                //收货人姓名
                $map['b.get_username']=array('like',"%$_REQUEST[name]%");
            }elseif($_REQUEST['type']==3){
                //收货人手机号
                $map['b.get_mobile']=array('like',"%$_REQUEST[name]%");
            }
        }

        if($_REQUEST['status']){
            if($_REQUEST['status']==11){
                $map['a.status']=array('eq',0);
            }else{
                $map['a.status']=array('gt',0);
            }
        }
        $count=M('after a')->join('teach_order b on a.order_id=b.id')->join('teach_vice_order c on c.order_id=b.id')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $list=M('after a')->join('teach_order b on a.order_id=b.id')->join('teach_vice_order c on c.order_id=b.id')->limit($Page->firstRow.','.$Page->listRows)->field('a.*,b.id ids,b.order_sn,b.pay_price,b.get_mobile,b.get_username')->where($map)
        ->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[order_id]")->select();
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            if($v['status']==0){
                $list[$k]['statuss']='未处理';
            }elseif($v['status']==1){
                $list[$k]['statuss']='售后中';
            }elseif($v['status']==2){
                $list[$k]['statuss']='售后成功';
            }elseif($v['status']==3){
                $list[$k]['statuss']='售后失败';
            }
        }
        // echo '<pre>';print_r($list);die;
        $this->assign('type',$_REQUEST['type']);
        $this->assign('name',$_REQUEST['name']);
        $this->assign('status',$_REQUEST['status']);
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);
        $this->assign('cur','售后订单');
        $this->display();
    }
            //修改售后状态
    public function state(){
        $id=$_POST['id'];
        $state=$_POST['state'];
        $order_id=M('after')->where("id=$id")->getField('order_id');
        if($state==1){
            $res=M('after')->where("id=$id")->save(['status'=>'2']);
            //订单表有个售后状态也要改
            M('order')->where("id=$order_id")->save(['status'=>'4']);
        }else{
            $res=M('after')->where("id=$id")->save(['status'=>'3']);
            //订单表有个售后状态也要改
            M('order')->where("id=$order_id")->save(['status'=>'4']);
        }
        if($res){
            

            $value=['code'=>'1','info'=>'修改成功'];
        }else{
            $value=['code'=>'2','info'=>'修改失败'];
        }
        exit(json_encode($value));
    }
            //收益管理(弃用)
    public function transaction_index(){
        $map['a.status']=array('between',"1,6");//大于0，小于7
        $_REQUEST['status']=array('between',"1,6");


        $supplier_id=session('supplier_id');
        $map['a.supplier_id']=session('supplier_id');
        $map['a.type']=4;
        // $map['status']=array('gt',0);//未付款的就没必要统计
        $_REQUEST['name'] ? $map['a.order_sn'] = array('like', "%$_REQUEST[name]%") : '';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime = strtotime($_REQUEST['start_time']);
            $etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
            $map['a.create_time'] = array('between', "$btime,$etime");
        }

        $map['b.status']=array('neq',2);
        $count=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($map)->field('a.*')->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $list=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($map)->field('a.*')->limit($Page->firstRow.','.$Page->listRows)
        ->order('a.id desc')->select();

        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['status'] == 0){
                $list[$k]['statuss']='待付款';
            }else if($v['status'] == 1){
                $list[$k]['statuss']='待发货';
            }else if($v['status'] == 2){
                $list[$k]['statuss']='待收货';
            }else if($v['status'] == 3){
                $list[$k]['statuss']='待评价';
            }else if($v['status'] == 4){
                $list[$k]['statuss']='已完成';
            }else if($v['status'] == 5){
                $list[$k]['statuss']='已过期';
            }else if($v['status'] == 6){
                $list[$k]['statuss']='售后';
            }else if($v['status'] == 7){
                $list[$k]['statuss']='订单取消退款中';
            }else if($v['status'] == 8){
                $list[$k]['statuss']='已取消';
            }
        }


        //今日收入
        $today_price=0;
        $condition['a.supplier_id']=$supplier_id;
        $condition['a.type']=4;
        // $condition['status']=array('gt',0);//未付款的就没必要统计
        $condition['a.status']=array('between',"1,6");
        $betime=strtotime(date('Y-m-d',time()));//凌晨
        $time=date('Y-m-d',time());
        $entime=strtotime($time. " 23:59:59");
        $condition['a.create_time']=array('between', "$betime,$entime");
        $condition['b.status']=array('neq',2);
        $t_p=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($condition)->field('a.*')->select();
        foreach($t_p as $k=>$v){
            $today_price+=$v['pay_price'];
        }
        //本月收入
        unset($condition['a.create_time']);
        $m_price=0;
        $m=date('Y-m',time());//这个月的起始日期

        $bt=strtotime($m);

        $et=time();
        $condition['a.create_time']=array('between', "$bt,$et");
        $m_p=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($condition)->field('a.*')->select();
        foreach($m_p as $k=>$v){
            $m_price+=$v['pay_price'];
        }
        //总收入
        unset($condition['a.create_time']);
        $c_price=0;
        $count_p=M('order a')->join('left join teach_after b on a.id=b.order_id')->where($condition)->field('a.*')->select();
        foreach($count_p as $k=>$v){
            $c_price+=$v['pay_price'];
        }
        $this->assign('c_price',$c_price);
        $this->assign('m_price',$m_price);
        $this->assign('today_price',$today_price);
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);

        $this->assign('cur','收益管理');
        $this->display();
    }

    public function transaction_index123(){
        $supplier_id=session('supplier_id');
        $map['b.supplier_id']=session('supplier_id');
        $map['a.type']=4;
        $map['a.status']=array('gt',0);//未付款的就没必要统计
        $_REQUEST['name'] ? $map['a.order_sn'] = array('like', "%$_REQUEST[name]%") : '';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime = strtotime($_REQUEST['start_time']);
            $etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
            $map['a.create_time'] = array('between', "$btime,$etime");
        }
        $count=M('order a')->join('teach_vice_order b on a.id=b.order_id')->where($map)->count();
        $Page      = new \Think\Page($count,10,$_REQUEST);
        $show      = $Page->show();
        $list=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where($map)->limit($Page->firstRow.','.$Page->listRows)
        ->order('a.id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['good_list']=M('vice_order')->where("order_id=$v[id]")->select();
            $list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $list[$k]['mobile']=M('ucenter_member')->where("id=$v[uid]")->getField('mobile');
            if($v['pay_type']==1){
                $list[$k]['pay_type']='微信';
            }else{
                $list[$k]['pay_type']='支付宝';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['status'] == 0){
                $list[$k]['statuss']='待付款';
            }else if($v['status'] == 1){
                $list[$k]['statuss']='待发货';
            }else if($v['status'] == 2){
                $list[$k]['statuss']='待收货';
            }else if($v['status'] == 3){
                $list[$k]['statuss']='待评价';
            }else if($v['status'] == 4){
                $list[$k]['statuss']='已完成';
            }else if($v['status'] == 5){
                $list[$k]['statuss']='已过期';
            }else if($v['status'] == 6){
                $list[$k]['statuss']='售后';
            }else if($v['status'] == 7){
                $list[$k]['statuss']='订单取消退款中';
            }else if($v['status'] == 8){
                $list[$k]['statuss']='已取消';
            }
        }


        //今日收入
        $today_price=0;
        $condition['b.supplier_id']=$supplier_id;
        $condition['a.type']=4;
        $condition['a.status']=array('gt',0);//未付款的就没必要统计
        $betime=strtotime(date('Y-m-d',time()));//凌晨
        $time=date('Y-m-d',time());
        $entime=strtotime($time. " 23:59:59");
        $condition['a.create_time']=array('between', "$betime,$entime");
        $t_p=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where($condition)->field('a.*')->select();
        foreach($t_p as $k=>$v){
            $today_price+=$v['pay_price'];
        }
        //本月收入
        unset($condition['a.create_time']);
        $m_price=0;
        $m=date('Y-m',time());//这个月的起始日期

        $bt=strtotime($m);

        $et=time();
        $condition['a.create_time']=array('between', "$bt,$et");
        $m_p=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where($condition)->field('a.*')->select();
        foreach($m_p as $k=>$v){
            $m_price+=$v['pay_price'];
        }
        //总收入
        unset($condition['a.create_time']);
        $c_price=0;
        $count_p=M('order a')->join('teach_vice_order b on a.id=b.order_id')->group('a.id')->where($condition)->field('a.*')->select();
        foreach($count_p as $k=>$v){
            $c_price+=$v['pay_price'];
        }
        $this->assign('c_price',$c_price);
        $this->assign('m_price',$m_price);
        $this->assign('today_price',$today_price);
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->assign('msg',$_REQUEST);

        $this->assign('cur','收益管理');
        $this->display();
    }

    public function save_sku(){
        $add_data['one_type_id']=$_POST['one_type_id'];
        $add_data['two_type_id']=$_POST['two_type_id'];
        $add_data['name']=$_POST['name'];
        $add_data['press']=$_POST['press'];
        $add_data['type']=$_POST['type'];
        if($_POST['type']==2){
            foreach($_POST['good_id'] as $k=>$v){
                if($k==0){
                    $add_data['good_ids'].=$v;
                }else{
                    $add_data['good_ids'].=','.$v;
                }
            }
        }
        $add_data['author']=$_POST['author'];
        $add_data['publication_time']=$_POST['publication_time'];
        $add_data['detail']=$_POST['detail'];
        $add_data['imgs']=$_POST['imgs'];
        //供应商ID
        $add_data['supplier_id']=session('supplier_id');
        $iid=$_POST['iid'];
        $list=M('item_attr_key')->where("item_id=$iid")->order("attr_key_id asc")->select();
        foreach($list as $k=>$v){
            $sub[$k]['name']=$v['attr_name'];
            $gg=M('item_attr_val')->where("attr_key_id=$v[attr_key_id]")->field('attr_value')->select();
            foreach($gg as $kk=>$vv){
                    $sub[$k]['spec'][]=$vv['attr_value'];
            }
        }
        $bb=json_encode($sub,JSON_UNESCAPED_UNICODE);
        $add_data['create_time']=time();
        $add_data['spec']=$bb;
        $res=M('goods')->add($add_data);
        $chk_value=$_POST['chk_value'];
        if($res){
            foreach($_POST['obj'] as $k=>$v){
                $add['goods_id']=$res;
                $add['old_price']=$v['original_price'];
                $add['price']=$v['original_price'];
                $add['count']=$v['stock'];
                $add['code']=$v['code'];
                $symbol=explode(',', $v['symbol']);
                $add['spec']=json_encode($symbol,JSON_UNESCAPED_UNICODE);
                $add['img']=$chk_value[$k];
                // $price[$k]=$v['price'];
                $price[$k]=$v['original_price'];//取的成本价  成本价跟售价是一个价，因为总后台才能定售价
                //还要库存 编码 图片
                M('goods_spec')->add($add);
            }
            $min_price=min($price);
            
            $max_price=max($price);

            M('goods')->where("id=$res")->save(['min_price'=>$min_price,'max_price'=>$max_price]);
            $value=['code'=>'100','msg'=>'成功'];
        }else{
            $value=['code'=>'200','msg'=>'失败'];
        }
        exit(json_encode($value));
    }

    public function save_skuu(){
        // print_r($_POST);die;
        $good_id=$_POST['iid'];
        M('goods_spec')->where("goods_id=$good_id")->delete();
        $add_data['one_type_id']=$_POST['one_type_id'];
        $add_data['press']=$_POST['press'];
        $add_data['two_type_id']=$_POST['two_type_id'];
        $add_data['name']=$_POST['name'];
        $add_data['type']=$_POST['type'];
        if($_POST['type']==2){
            foreach($_POST['good_id'] as $k=>$v){
                if($k==0){
                    $add_data['good_ids'].=$v;
                }else{
                    $add_data['good_ids'].=','.$v;
                }
            }
        }else{
            $add_data['good_ids']='';
        }
        $add_data['author']=$_POST['author'];
        $add_data['publication_time']=$_POST['publication_time'];
        $add_data['detail']=$_POST['detail'];
        $add_data['imgs']=$_POST['imgs'];
        //供应商ID  缺少
        $iid=$_POST['iid'];
        $list=M('item_attr_key')->where("item_id=$iid")->order("attr_key_id asc")->select();
        foreach($list as $k=>$v){
            $sub[$k]['name']=$v['attr_name'];
            $gg=M('item_attr_val')->where("attr_key_id=$v[attr_key_id]")->field('attr_value')->select();
            foreach($gg as $kk=>$vv){
                    $sub[$k]['spec'][]=$vv['attr_value'];
            }
        }
        $bb=json_encode($sub,JSON_UNESCAPED_UNICODE);
        $add_data['create_time']=time();
        $add_data['spec']=$bb;
        $res=M('goods')->where("id=$good_id")->save($add_data);
        $chk_value=$_POST['chk_value'];
        if($res){
            foreach($_POST['obj'] as $k=>$v){
                $add['goods_id']=$good_id;
                $add['old_price']=$v['original_price'];
                $add['price']=$v['original_price'];
                $add['count']=$v['stock'];
                $add['code']=$v['code'];
                $symbol=explode(',', $v['symbol']);
                $add['spec']=json_encode($symbol,JSON_UNESCAPED_UNICODE);
                $add['img']=$chk_value[$k];
                // $price[$k]=$v['price'];
                $price[$k]=$v['original_price'];
                //还要库存 编码 图片
                M('goods_spec')->add($add);
            }
            $min_price=min($price);
            
            $max_price=max($price);

            M('goods')->where("id=$res")->save(['min_price'=>$min_price,'max_price'=>$max_price]);
            $value=['code'=>'100','msg'=>'成功'];
        }else{
            $value=['code'=>'200','msg'=>'失败'];
        }
        exit(json_encode($value));
    }

    public function save_goods(){
        // echo '<pre>';print_r($_POST);die;
        $id=$_POST['iid'];
        $add_data['one_type_id']=$_POST['one_type_id'];
        $add_data['two_type_id']=$_POST['two_type_id'];
        $add_data['name']=$_POST['name'];
        $add_data['type']=$_POST['type'];
        $add_data['author']=$_POST['author'];
        $add_data['publication_time']=$_POST['publication_time'];
        $add_data['detail']=$_POST['detail'];
        $add_data['imgs']=$_POST['imgs'];
        $add_data['update_time']=time();
        if($_POST['type']==2){
            foreach($_POST['good_id'] as $k=>$v){
                if($k==0){
                    $add_data['good_ids'].=$v;
                }else{
                    $add_data['good_ids'].=','.$v;
                }
            }
        }else{
            $add_data['good_ids']='';
        }
        $res=M('goods')->where("id=$id")->save($add_data);
        if($res){
            $price=$_POST['price'];
            $old_price=$_POST['old_price'];
            $code=$_POST['code'];
            $count=$_POST['count'];
            $img=$_POST['img'];
            $list=M('goods_spec')->where("goods_id=$id")->order('id asc')->select(); 
            foreach($list as $k=>$v){
                M('goods_spec')->where("id=$v[id]")->save(['price'=>$price[$k],'old_price'=>$old_price[$k],'code'=>$code[$k],'count'=>$count[$k],'img'=>$img[$k]]);
            }
            $value=['code'=>'100','msg'=>'成功'];
        }else{
            $value=['code'=>'200','msg'=>'失败'];
        }
        exit(json_encode($value));
    }

        public function save_attr()
    {
        if($_POST){
            $data=$_POST;
            $key=json_decode($data['key'],true);
            $value=json_decode($data['value'],true);


            $item_id=$data['good_id'];

            $key_id=[];
            

            M('item_attr_key')->where(['item_id'=>$item_id])->delete();
            foreach ($key as $k) {

                $attr_key=M('item_attr_key')->where(['attr_name'=>$k,'item_id'=>$item_id])->find();
                if(!$attr_key){
                    $attr_key=M('item_attr_key');
                    $attr_key->attr_name=$k;
                    $attr_key->item_id=$item_id;
                    $attr_key_id=$attr_key->add();

                }
                $key_id[]=$attr_key_id;
            }
            $tm_v_in=[];
            $tm_v=[];
            M('item_attr_val')->where(['item_id'=>$item_id])->delete();
            foreach ($value as $key=>$v){
                $attr_key_id=$key_id[$key];
                foreach ($v as $v1){
                    $attr_value=M('item_attr_val')->where(['attr_value'=>$v1,'attr_key_id'=>$attr_key_id])->find();
                    if(!$attr_value){
                        $attr_value=M('item_attr_val');
                        $attr_value->attr_key_id=$attr_key_id;
                        $attr_value->attr_value=$v1;
                        $attr_value->item_id=$item_id;
                        $symbol=$attr_value->add();
                    }
                    // $tm_v[]=$symbol;//这是编号
                    $tm_v[]=$v1;//这是汉字
 
                }
//                $tm_v[]=$tm_v_in;
            }
 
            $this->success('请求成功',['key'=>$key_id,'value'=>$tm_v]);
 
        }
 
        $this->success('请求成功');
    }
        public function uploadPicture()
    {
        /*if (!is_login()) {
            exit;
        }*/
        /* 返回标准数据 */
        if (!is_dir("Uploads/" . date("Y-m-d"))) {
            mkdir("Uploads/" . date("Y-m-d"));
        }
        $file = $_FILES['download'];
        $str = stristr($file['name'], '.');

        if ($str != '') {
            // $path = "Uploads/Picture/" . date("Y-m-d") . "/" . time()  ."_" . rand(1000,9999) . "_" . is_login() . '_himg.jpg';
           $path ="Uploads/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . '.'.$str;
        } else {
            $path = '';
        }

        if ($path != '') {
            copy($file['tmp_name'], $path);
            $this->thumbImg($path, 500, 500);
        }
        //插入图片表
        $data['path'] = '/' . $path;
        $data['status'] = 1;
        $data['create_time'] = time();
        $cover_id = D('picture')->add($data);
        $return = array('status' => 1, 'info' => '上传成功', 'id' => $cover_id, 'path' => $data['path']);
        /* 调用文件上传组件上传文件 */
        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    function thumbImg($imgPath,$width=255,$height=255){
    $Image=new \Think\Image(1,$imgPath);
    $Image->thumb($width,$height);
    $Image->save($imgPath);
}
    public function login_out(){
        session_destroy();
        $this->success('退出成功',U('login'));
    }
        public function uploadImg() {
        if (empty($_FILES) ){
            echo json_encode(['code'=>1,'msg'=>'图片不能为空']);exit;
        }else{
            $file = $_FILES['image'];
            if (!is_dir("Uploads/" . date("Y-m-d"))) {
                mkdir("Uploads/" . date("Y-m-d"));
            }
            if (is_uploaded_file($file['tmp_name'])) {

                $str = stristr($file['name'], '.');
                $str=substr($str,1);
                $avatar_path = "Uploads/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . '.' . $str;

                if(copy($file['tmp_name'],$avatar_path)){
// print_r($file['tmp_name']);die;
                    $game_path = str_replace('Uploads/','game/headimage/',$avatar_path);
                    copy($file['tmp_name'],$game_path);
                    $cover['path'] = "/".$avatar_path;
                    $cover['status'] = 1;
                    $cover['create_time'] = time();
                    $re = M('picture')->add($cover);
                    if($re){
                        $img=get_host() . get_cover($re, "path");
                        $hh['imgurl']=$img;
                        $hh['imgid']=$re;
                        echo json_encode(['code'=>0,'msg'=>'成功','info'=>$hh]);exit;
                    }else{
                        echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                    }
                }else {
                    echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                }
            }
        }
    }
        public function getCourselist()
    {
        $category = $_REQUEST['category'];//1面授，2点播，3直播
        $course_id = $_REQUEST['course_id'];
        // print_r($course_id);die;
        //         Array
        // (
        //     [0] => 2
        //     [1] => 1
        // )


            foreach ($course_id as $k => $v) {
                // $course[$k]['category']=4;
                // $course[$k]['id']=$v;
                $course[$k] = D('goods')->where(array('id' => $v))->find();
                $imgg = explode(',', $course[$k]['imgs']);
                $course[$k]['img'] = $imgg[0];
                $course[$k]['category_name'] = '书籍';
                $course[$k]['category'] = 4;
            }
        //tzb 处理下图片
        foreach ($course as $k => $v) {
            $course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
        }

        return api_return(1, '信息获取成功', $course);
    }
    public function getCourse()
    {
        $supplier_id=session('supplier_id');
        // print_r($_POST);die;
        $type_id = $_REQUEST['type_id'];
        // $iid = $_REQUEST['iid'];//机构ID
        $iid = session('iid');
        //新加一个课程名称搜索
        $_REQUEST['name'] ? $name = array('like', "%$_REQUEST[name]%") : $name = '';


            if ($name) {
                $course = D('goods')->where(array('two_type_id' => $type_id, 'name' => $name, 'is_del' => 0,'supplier_id'=>$supplier_id))->order('id desc')->select();
            } else {

                $course = D('goods')->where(array('two_type_id' => $type_id, 'is_del' => 0,'supplier_id'=>$supplier_id))->order('id desc')->select();
                foreach ($course as $k => $v) {
                    $imgg = explode(',', $v['imgs']);
                    $course[$k]['img'] = $imgg[0];
                }
            }
        // print_r($course);die;

        //tzb 处理下图片
        foreach ($course as $k => $v) {
            $img=M('goods_spec')->where("goods_id=$v[id]")->getField('img');
            if($img){
                $course[$k]['img'] = M('picture')->where("id=$img")->getfield('path');
            }else{
                $course[$k]['img'] = '';
            }
            
        }
        // print_r($course);die;
        return api_return(1, '信息获取成功', $course);
    }
        //tzb 书籍分类
    public function getChildTypes()
    {

        $pid = $_REQUEST['pid'];

        $child = D('goods_type')->where(array('pid' => $pid))->order('id desc')->select();


        return api_return(1, '信息获取成功', $child);
    }
}
